package org.mf.security.filter;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.filter.OncePerRequestFilter;

public class BlacklistFilter extends OncePerRequestFilter {

	@Override
	protected void doFilterInternal(HttpServletRequest request,
			HttpServletResponse response, FilterChain filterChain)
			throws ServletException, IOException {
		Authentication authentication = SecurityContextHolder.getContext()
				.getAuthentication();
		if (authentication != null) {
			// If the username is equal to mike, deny access
			if (authentication.getName().equals("mike") == true) {
				logger.error("Username and password match. Access denied!");
				throw new AccessDeniedException(
						"Username and password match. Access denied!");
			}

		}

		// User details are not empty
		logger.debug("Continue with remaining filters");
		filterChain.doFilter(request, response);
	}

}
